package com.link.base.base.dailystatement.controller;


import com.link.base.user.model.CoreUser;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.util.UserUtil;
import com.link.base.base.dailystatement.model.DailyStatement;
import com.link.base.base.dailystatement.service.DailyStatementService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.DateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  安小波
 * @date:  2018-07-30
 * @version 1.0
 */

@Controller
@RequestMapping("/link/dailyStatement")
public class DailyStatementController extends BasicController<DailyStatement> {

    @Resource
    private DailyStatementService dailyStatementService;

    @Override
    public BasicService<DailyStatement> getBasicService() throws Exception {
        return dailyStatementService;
    }

    /**
     * 新建一条日结头信息，新建直接先校验
     * 是否存在已有的日结头信息
     *
     * @param qps
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/newDaily", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> newDaily(@JsonParam QueryParams qps,@JsonParam DailyStatement entity, HttpSession session,
                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            CoreUser user = UserUtil.getUser();
            if (user == null) {
                throw new ServiceException("PUBLIC-035");
            }
            entity.setOrgId(user.getOrgId());

            //查询当日日结单
            List<DailyStatement> dailyList = dailyStatementService.queryCurrentDaily(entity);
            //当日没有日结单则新建一条日结单
            if (dailyList.size() <= 0) {
                String dailyDate = DateUtil.dateToStr(new Date(), "yyyy-MM-dd");
                entity.setDailyDate(dailyDate);
                entity.setStartTime("00:00");
                entity.setEndTime("24:00");
                entity.setClerk(user.getFirstName());
                //状态为待确认
                entity.setStatus("Unconfirmed");
                dailyStatementService.insert(entity);
                DailyStatement newDaily = dailyStatementService.queryById(entity);
                result.put("newRow", newDaily);
                result.put("isDaily", true);
                result.put("success", true);
            } else {
                DailyStatement daily = dailyList.get(0);
                //如果状态为已完成则直接返回
                String finished = "finished";
                String unconfirmed = "Unconfirmed";
                if (finished.equals(daily.getStatus())) {
                    result.put("isDaily", false);
                    result.put("success", true);
                    return result;
                }
                //状态为待确定的则返回这条记录
                if (unconfirmed.equals(daily.getStatus())) {
                    result.put("isDaily", true);
                    result.put("newRow", daily);
                    result.put("success", true);
                }
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 跟新日结单的备注、状态、提交时间
     *
     * @param qps
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/dailyInfoUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> dailyInfoUpdate(@JsonParam QueryParams qps, @JsonParam DailyStatement entity,
                                               HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            dailyStatementService.dailyInfoUpdate(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
